import {
  _decorator,
  Color,
  color,
  Component,
  Node,
  randomRange,
  Sprite,
  UITransform,
  v3,
} from "cc";
const { ccclass, property } = _decorator;

@ccclass("Food")
export class Food extends Component {
  start() {
    this.node.getComponent(Sprite).color = this.randomColor();
    this.node.setPosition(this.randomPos());
  }

  update(deltaTime: number) {}

  randomColor() {
    const colorRange = randomRange(0, 255);
    return new Color(colorRange, colorRange, colorRange);
  }

  randomPos() {
    const offSetDis = 200; // 防止隔墙太近
    const width = this.node.parent.getComponent(UITransform).width - offSetDis;
    const height =
      this.node.parent.getComponent(UITransform).height - offSetDis;
    const randomX = Math.round(Math.random() * width - width / 2);
    const randomY = Math.round(Math.random() * height - height / 2);
    const randomPos = v3(randomX, randomY, 0);
    return randomPos;
  }
}
